package DataStructure.Stack;

/**
 * @Author：DijaH
 * @Date：2023/2/28 9:13 下午
 * @Version：1.0.0
 * @Description： 数组实现栈
 */
public class ArrayStack {
    private Integer[] arr;
    private Integer size;
    public ArrayStack(int initsize){
        if(initsize < 0){
            throw new IllegalArgumentException("this init size is less than 0");
        }
        arr = new Integer[initsize];
        size = 0;
    }
    public Integer peek(){
        if(size == 0){
            return null;
        }
        return arr[size-1];
    }
    public void push(int obj){
        if(size == arr.length){
            throw new ArrayIndexOutOfBoundsException("this stack is full");
        }
        arr[size++] = obj;
    }
    public Integer pop(){
        if(size == 0){
            throw new ArrayIndexOutOfBoundsException("this stack is empty");
        }
        return arr[--size];
    }

}
